package com.jd.sunny.java;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description:enum usage
 * @author:sunlin
 * @since:2012-11-28 14:11
 * @version:1.0.0
 */
public enum WeekEnum {

    //user , to separate the last use ;
    NULL(null, null, null),
    Monday(1,"monday","monday"),
    Tuesday(2,"tuesday","tuesday"),
    Wednesday(3,"wednesday","wendesday"),
    Thursday(4,"thursday","thursday"),
    Friday(5,"friday","friday"),
    Saturday(6,"saturday","saturday"),
    Sunday(7,"sunday","sunday");


    /**
     * code
     */
    private Integer code;
    /**
     * name
     */
    private String name;
    /**
     * description
     */
    private String description;

    private WeekEnum(Integer code){
        this.code = code;
    }
    private WeekEnum(Integer code,String name){
        this.code = code;
        this.name = name;
    }
    private  WeekEnum(String name,String description){
        this.name = name;
        this.description = description;
    }
    private WeekEnum(Integer code,String name,String description){
        this.code = code;
        this.description = description;
    }

    /**
     * to code
     * @return
     */
    public Integer toCode(){
        return this.code ==null?this.ordinal():this.code;
    }

    /**
     * to name
     * @return
     */
    public String toName(){
        return this.name ==null?this.name():this.name;
    }

    /**
     * to Description
     * @return
     */
    public String toDescription(){
        return  this.description;
    }

    /**
     * to string
     * @return
     */
    public String toString(){
        return this.name() + "(" +toCode() + ",\"" +  toName() + "\",\"" + toDescription() + "\")";
    }

    /**
     *get enum from code
     * @param code
     * @return WeekEnum
     */
    public static WeekEnum enumValueOf(Integer code) {
        WeekEnum[] values = WeekEnum.values();
        WeekEnum v = NULL;
        for (int i = 0; i < values.length; i++) {
            if (code != null && code.equals(values[i].toCode())) {
                v = values[i];
                break;
            }
        }
        return v;
    }

    /**
     * get enum from name
     *
     * @param name
     * @return
     */
    public static WeekEnum enumValueOf(String name) {
        WeekEnum[] values = WeekEnum.values();
        WeekEnum v = NULL;
        for (int i = 0; i < values.length; i++) {
            if (name != null && name.equalsIgnoreCase(values[i].toName())) {
                v = values[i];
                break;
            }
        }
        return v;
    }

    /**
     * get all <code,description> map
     *
     * @return
     */
    public static Map<Integer, String> toCodeDescriptionMap() {
        LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
        for (int i = 0; i < WeekEnum.values().length; i++) {
            if(WeekEnum.values()[i] != NULL){
                map.put(WeekEnum.values()[i].toCode(), WeekEnum.values()[i].toDescription());
            }
        }
        return map;
    }

    /**
     * get all <name,description> map
     *
     * @return
     */
    public static Map<String, String> toNameDescriptionMap() {
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        for (int i = 0; i < WeekEnum.values().length; i++) {
            if(WeekEnum.values()[i] != NULL){
                map.put(WeekEnum.values()[i].toName(), WeekEnum.values()[i].toDescription());
            }
        }
        return map;
    }

}

class TestEnum{

    public static void main(String[]args){
        System.out.println(WeekEnum.Sunday.toString());
    }

}